import threading
import socket


def send_msg(udp_socket):
    """udp发送数据"""
    while True:
        send_data = input("请输入要发送的数据:")
        udp_socket.sendto(send_data.encode("gbk"), ("192.168.159.1", 8080))


def recv_msg(udp_socket):
    while True:
        recv_data = udp_socket.recvfrom(1024)
        print(recv_data)


def main():
    # 创建socket
    udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    # 绑定地址
    udp_socket.bind(("", 7890))

    # 接收数据
    t_recv = threading.Thread(target=recv_msg, args=(udp_socket,))
    # 发送数据
    t_send = threading.Thread(target=send_msg, args=(udp_socket,))

    t_recv.start()
    t_send.start()


if __name__ == '__main__':
    main()
